//
//  ViewControllerOfHotWindow.m
//  Beauty
//
//  Created by Snow on 16/9/13.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "ViewControllerOfHotWindow.h"
#import "GXMacro.h"
#import "GXNetWorkingParser.h"
#import "UIView+Frame.h"
#import "TableViewCellOfHotWindow.h"
#import "TableViewCellOfComment.h"
#import "ModelOfComment.h"
@interface ViewControllerOfHotWindow ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *arrayOfVC;
@property (nonatomic, strong) NSDictionary *dictionay;
@end

@implementation ViewControllerOfHotWindow

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"22";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"more_white"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]style:UIBarButtonItemStylePlain target:self action:@selector(handleRightBarButtonItem:)];
    self.navigationItem.title = @"瞬间详情";
    [self handleData];
    [self createView];
    [self createTableView];
}
- (void)handleData
{
    _arrayOfVC = [NSMutableArray array];
    //http://www.wantease.com/api/index.php?act=micro&op=inspirationDetail&key=979742e53b1003df4b0ecce6ba8a1168&personal_id=1100
    NSString *string = [NSString stringWithFormat:@"http://www.wantease.com/api/index.php?act=micro&op=inspirationDetail&personal_id=%@", _person_id];
    [GXNetWorkingParser GETWithURL:string par:nil success:^(id responseObject) {
        NSDictionary *dic = responseObject[@"datas"];
        _dictionay = [NSDictionary dictionaryWithDictionary:dic[@"micro_personal_detail"]];
        NSArray *arr = dic[@"micro_comment_list"];
        for (NSDictionary *dict in arr) {
            ModelOfComment *model = [[ModelOfComment alloc]init];
            [model setValuesForKeysWithDictionary:dict];
            [_arrayOfVC addObject:model];
        }
        UIView *viewOfHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 400)];
        UIImageView *imageOfPerson = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 40, 40)];
        imageOfPerson.layer.masksToBounds = YES;
        imageOfPerson.layer.cornerRadius = 20;
        [imageOfPerson sd_setImageWithURL:[NSURL URLWithString:_dictionay[@"member_avatar"]]];
        [viewOfHeader addSubview:imageOfPerson];
        UILabel *labelOfName = [[UILabel alloc] initWithFrame:CGRectMake(60, 10, SCREEN_WIDTH - 60, 20)];
        labelOfName.text = _dictionay[@"member_truename"];
        [viewOfHeader addSubview:labelOfName];
        UILabel *labelOfID = [[UILabel alloc] initWithFrame:CGRectMake(60, 30, SCREEN_WIDTH - 60, 20)];
        labelOfID.text = _dictionay[@"member_name"];
        labelOfID.font = [UIFont systemFontOfSize:15];
        labelOfID.textColor = [UIColor lightGrayColor];
        [viewOfHeader addSubview:labelOfID];
        UIImageView *imageOfContent = [[UIImageView alloc] initWithFrame:CGRectMake(0, 60, SCREEN_WIDTH, 340)];
        [imageOfContent sd_setImageWithURL:[NSURL URLWithString:_dictionay[@"commend_image"]]];
        [viewOfHeader addSubview:imageOfContent];
        _tableView.tableHeaderView = viewOfHeader;
        
        [_tableView reloadData];
    } filed:^(NSError *error) {
    
    }];
}
- (void)createView
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 50 - 64, SCREEN_WIDTH, 50)];
    view.backgroundColor = [UIColor whiteColor];
    
    UIButton *buttonOfShare = [UIButton buttonWithType:UIButtonTypeCustom];
    [buttonOfShare setImage:[UIImage imageNamed:@"share"] forState:UIControlStateNormal];
    buttonOfShare.frame = CGRectMake(10, 10, 30, 30);
    [view addSubview:buttonOfShare];
    
    UILabel *labelOfShare = [[UILabel alloc] initWithFrame:CGRectMake(40, 15, 20, 20)];
    labelOfShare.text = @"4";
    [view addSubview:labelOfShare];
    
    UIButton *buttonOfLike = [UIButton buttonWithType:UIButtonTypeCustom];
    buttonOfLike.frame = CGRectMake(SCREEN_WIDTH / 4, 10, 30, 30);
    [buttonOfLike setImage:[UIImage imageNamed:@"buttonOfLove"] forState:UIControlStateNormal];
    [view addSubview:buttonOfLike];
    UILabel *labelOfLike = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 4 + 30, 15, 20, 20)];
    labelOfLike.text = @"5";
    [view addSubview:labelOfLike];
    
    UIButton *buttonOfComment = [UIButton buttonWithType:UIButtonTypeCustom];
    buttonOfComment.frame = CGRectMake(SCREEN_WIDTH / 2 - 10, 10, 30, 30);
    [buttonOfComment setImage:[UIImage imageNamed:@"conment"] forState:UIControlStateNormal];
    [view addSubview:buttonOfComment];
    UILabel *labelOfComment = [[UILabel alloc] initWithFrame:CGRectMake(25 + SCREEN_WIDTH / 2, 15, 30, 20)];
    labelOfComment.text = @"评论";
    labelOfComment.font = [UIFont systemFontOfSize:15];
    [view addSubview:labelOfComment];
    
    UIButton *buttonOfAdd = [UIButton buttonWithType:UIButtonTypeCustom];
    buttonOfAdd.frame = CGRectMake(SCREEN_WIDTH / 4 * 3 - 10, 0, SCREEN_WIDTH / 4 + 10, 50);
    buttonOfAdd.backgroundColor = [UIColor colorWithRed:0.365 green:0.392 blue:0.475 alpha:1.000];
    [buttonOfAdd setImage:[UIImage imageNamed:@"AppIcon40x40"] forState:UIControlStateNormal];
    [buttonOfAdd setTitle:@"加入橱窗" forState:UIControlStateNormal];
    buttonOfAdd.titleLabel.font = [UIFont systemFontOfSize:15];
    [view addSubview:buttonOfAdd];
    [self.view addSubview:view];

}
- (void)createTableView
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView registerClass:[TableViewCellOfHotWindow class] forCellReuseIdentifier:@"TableViewCellOfHotWindow"];
    [_tableView registerClass:[TableViewCellOfComment class] forCellReuseIdentifier:@"TableViewCellOfComment"];
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    } else {
    return _arrayOfVC.count;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0) {
    TableViewCellOfHotWindow *cell = [tableView dequeueReusableCellWithIdentifier:@"TableViewCellOfHotWindow"];
        cell.label.text = _dictionay[@"commend_message"];
        cell.label.numberOfLines = 0;
    return cell;
    } else {
        ModelOfComment *model = [_arrayOfVC objectAtIndex:indexPath.row];
        TableViewCellOfComment *cell = [tableView dequeueReusableCellWithIdentifier:@"TableViewCellOfComment"];
        [cell.image sd_setImageWithURL:[NSURL URLWithString:model.member_avatar]];
        cell.labelOfName.text = model.member_name;
        cell.labelOfTime.text = model.comment_time;
        cell.labelOfomment.text = model.comment_message;
        
        cell.labelOfLikeCount.text = model.like_count;
        return cell;
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, SCREEN_WIDTH, 20)];
        label.text =[NSString stringWithFormat:@"查看%@次", _dictionay[@"click_count"]];
        [view addSubview:label];

        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(SCREEN_WIDTH - 60, 10, 20, 20);
        [view addSubview:button];
        [button setImage:[UIImage imageNamed:@"buttonOfLove"] forState:UIControlStateNormal];
        
        UILabel *labelOfLikeCount = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 30, 10, 20, 20)];
        labelOfLikeCount.text = _dictionay[@"like_count"];
        [view addSubview:labelOfLikeCount];
        return view;
    } else {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, SCREEN_WIDTH, 20)];
        label.text = @"全部评论";
        [view addSubview:label];
        return view;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 0) {
        return 0;
    } else {
        return 60;
    }
}
- (void)handleRightBarButtonItem:(UIBarButtonItem *)barButtonItem
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 100, 0, 0, 0)];
    [self.view addSubview:view];
    view.backgroundColor = [UIColor colorWithRed:0.286 green:0.306 blue:0.392 alpha:1.000];

    [UIView animateWithDuration:0.5 animations:^{
        view.x_width = 100;
        view.x_height = 100;
    }];
}

#pragma mark ------------  去掉tableView的头部分区悬浮效果  -----------
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat sectionHeaderHeight = 40;
    CGFloat offsetY = scrollView.contentOffset.y;
    if (offsetY >= 0 && offsetY <= sectionHeaderHeight)
    {
        scrollView.contentInset = UIEdgeInsetsMake(-offsetY, 0, 0, 0);
    }else if (offsetY >= sectionHeaderHeight && offsetY <= scrollView.contentSize.height - scrollView.frame.size.height)
    {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
